package se.ramfelt.psnfriends.model.web;

import java.util.ArrayList;

import se.ramfelt.psn.model.Friend;
import se.ramfelt.psn.model.TrophySummary;
import se.ramfelt.psn.model.Friend.Presence;
import se.ramfelt.psnfriends.R;
import se.ramfelt.psnfriends.R.string;

public class FakePsnCommunicator extends PsnCommunicator {

    private Listener listener;
    private boolean isloggedin = false;
	private int avatarIndex = 0;

    public FakePsnCommunicator(Listener listener) {
        super(null, null, null, listener);
        this.listener = listener;
    }

    @Override
    public void run() {
        try {
            listener.onStarted(this);
            if (!isloggedin) {
                listener.onStatus(this, R.string.status_psn_login);
                Thread.sleep(200);
                isloggedin = true;
            }
            listener.onStatus(this, R.string.status_psn_getfriendsxml);
            Thread.sleep(500);
            ArrayList<Friend> list = new ArrayList<Friend>();
            list.add(newFriend("Nate", Friend.Presence.Online, "Playing Little Big Planet"));
            list.add(newFriend("snake", Friend.Presence.Online, "Playing Uncharted 2"));
            list.add(newFriend("Sack boy", Friend.Presence.Online, "null"));
            list.add(newFriend("Clank", Friend.Presence.Offline, "Was last seen 4 days ago"));
            list.add(newFriend("Sev", Friend.Presence.Online, "Playing Killzone 3"));
            list.add(newFriend("niko", Friend.Presence.Online, "Playing Red Dead Redemption"));
            list.add(newFriend("Soap", Friend.Presence.Offline, "Was last seen 10 days ago"));
            //listener.onError(R.string.status_psn_unexpected_error, new InterruptedException("Fake interruption"));
            listener.onFinished(this, list);
            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private Friend newFriend(String onlineid, Friend.Presence presence, String currentGame) {
        Friend friend = new Friend(onlineid, presence, currentGame);
        friend.setLastSeenTimestamp(System.currentTimeMillis() - (long) (Math.random() * 3600 * 1000 * 24 * 10));
        friend.setTrophySummary(new TrophySummary((int) (Math.random() * 20), (int) (Math.random() * 5), (int) (Math.random() * 20), (int) (Math.random() * 50), (int) (Math.random() * 100)));
        switch (avatarIndex++) {
        case 0:
            friend.setAvatarUrl("/PSN_Images//avatar/3RD/30006.png");
        	break;
        case 1:
            friend.setAvatarUrl("/PSN_Images//avatar/SCEI/I0041.png");
        	break;
        case 2:
            friend.setAvatarUrl("/PSN_Images//avatar/WWS_A/A0016.png");
        	break;
        case 3:
            friend.setAvatarUrl("/PSN_Images//avatar/WWS_J/J0001.png");
        	break;
        case 4:
            friend.setAvatarUrl("/PSN_Images//avatar/WWS_E/E0040.png");
        	break;
        case 5:
            friend.setAvatarUrl("/PSN_Images//avatar/WWS_E/E0001.png");
        	break;
        case 6:
            friend.setAvatarUrl("/PSN_Images//avatar/3RD/30007.png");
        	break;
        }
        return friend;
    }

}
